<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 文生图</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea {
            width: 100%;
            min-height: 100px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        .select-group {
            margin-bottom: 15px;
        }
        select {
            padding: 8px;
            border-radius: 4px;
            border: 1px solid #ccc;
            width: 100%;
        }
        .result-area {
            margin-top: 30px;
        }
        .generated-image {
            max-width: 100%;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-top: 20px;
        }
        .download-btn {
            background-color: #2196F3;
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI 文生图系统</h1>
        <div class="form-group">
            <label for="promptInput">图像描述：</label>
            <textarea id="promptInput" placeholder="请输入您想要生成的图像描述..."></textarea>
        </div>
        <div class="form-group">
            <div class="select-group">
                <label for="modelSelect">选择模型：</label>
                <select id="modelSelect">
                    <option value="dall-e-3">DALL-E 3</option>
                    <option value="dall-e-2">DALL-E 2</option>
                    <option value="qwen-vl">阿里云通义千问</option>
                </select>
            </div>
            <div class="select-group">
                <label for="sizeSelect">图像尺寸：</label>
                <select id="sizeSelect">
                    <option value="1024x1024">1024x1024</option>
                    <option value="1024x1792">1024x1792 (竖向)</option>
                    <option value="1792x1024">1792x1024 (横向)</option>
                </select>
            </div>
            <div class="select-group">
                <label for="styleSelect">风格：</label>
                <select id="styleSelect">
                    <option value="natural">自然风格</option>
                    <option value="vivid">生动风格</option>
                    <option value="cartoon">卡通风格</option>
                    <option value="realistic">写实风格</option>
                </select>
            </div>
        </div>
        <button id="generateBtn" class="button">生成图像</button>
        
        <div id="resultArea" class="result-area" style="display: none;">
            <h2>生成结果</h2>
            <img id="generatedImage" class="generated-image">
            <div>
                <button id="downloadBtn" class="button download-btn">下载图像</button>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('generateBtn').addEventListener('click', async function() {
            const prompt = document.getElementById('promptInput').value.trim();
            const model = document.getElementById('modelSelect').value;
            const size = document.getElementById('sizeSelect').value;
            const style = document.getElementById('styleSelect').value;
            
            if (!prompt) {
                alert('请输入图像描述');
                return;
            }
            
            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在生成...';
            
            try {
                const response = await fetch('/api/generate_image', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        prompt: prompt,
                        model: model,
                        size: size,
                        style: style
                    })
                });
                
                const data = await response.json();
                
                if (data.image_url) {
                    document.getElementById('generatedImage').src = data.image_url;
                    document.getElementById('resultArea').style.display = 'block';
                } else {
                    alert('生成失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '生成图像';
            }
        });
        
        document.getElementById('downloadBtn').addEventListener('click', function() {
            const imageUrl = document.getElementById('generatedImage').src;
            if (!imageUrl) {
                alert('请先生成图像');
                return;
            }
            
            const a = document.createElement('a');
            a.href = imageUrl;
            a.download = 'generated-image.png';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        });
    </script>
</body>
</html> 